package org.fei.ClassDiagram;

import org.fei.ClassDiagram.GenerateDiagramAction;
import org.fei.ClassDiagram.RunAction;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.DynamicMenuContent;
import org.openide.filesystems.FileObject;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;

import java.net.URL;
import javax.swing.SwingUtilities;
import org.fei.ClassDiagram.ProjectScanner;
import org.openide.util.RequestProcessor;

/**
 * Trieda prida tlacitko Generate class diagram do kontextoveho menu pre projekty
 * 
 * Tlacitko sa zobrazi len v tom pripade ze dany projekt je typu Java SE.
 * 
 * @author Tomas
 */
@ActionID(id = "org.myorg.helloyou.DemoAction", category = "SomeFolder")
@ActionRegistration(displayName = "Generate Class diagram")
@ActionReference(path = "Projects/Actions")
public class GenerateDiagramProjectPopupAction extends AbstractAction implements ContextAwareAction {
    public @Override void actionPerformed(ActionEvent e) {assert false;}
    public @Override Action createContextAwareInstance(Lookup context) {
        return new ContextAction(context);
    }
    
    // vypinanie tlacitka
    private static boolean enabling = true;
    
    private static final class ContextAction extends AbstractAction {
        private final Project p;
        private ProjectScanner projectScanner;
        // background thread pool
        private static final RequestProcessor RP = new RequestProcessor(GenerateDiagramAction.class);
  
        public ContextAction(Lookup context) {
            p = context.lookup(Project.class);
            String name = ProjectUtils.getInformation(p).getDisplayName();
           
            ProjectScannerFactory.createProjectScanner(new ProjectScanner(p));
            projectScanner = ProjectScannerFactory.getProjectScanner();
            
            if (projectScanner.isProjectValid() && GenerateDiagramProjectPopupAction.enabling) {
                setEnabled(true);                
            }
            else {           
                setEnabled(false);
            }
            putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
            // TODO menu item label with optional mnemonics
            putValue(NAME, "Generate Class diagram");
        }

        public @Override void actionPerformed(ActionEvent e) {        
            // zalozit nove vlakno
            // vypnut tlacitko
            // na konci pracovneho vlakna zapnut akciu
            GenerateDiagramProjectPopupAction.setEnabling(false);
            RP.post(new RunAction(this, p), 0, Thread.NORM_PRIORITY);
        }
    }        
    
    public static void setEnabling(boolean enabling) {
            GenerateDiagramProjectPopupAction.enabling = enabling;
    }
}